package cn.edu.hunau.oop.day05.string;

import org.junit.Test;

import java.io.UnsupportedEncodingException;
import java.util.Arrays;

/**
 * @author hb
 * @date 2025-08-08 09:26:17
 */
public class StringTest {

    @Test
    public void testInit(){
     String s1=new String("abc");

     String s2= """
                第1行内容...
                第2行内容...
                """;

    }

    @Test
    public void testParse(){
        String url="www.hunau.edu.cn/xky/index.html?query=计科&keyword=web";
        //获得第一个 /的位置
        int index=url.indexOf("/");
        String host=url.substring(0,index);
        System.out.println(host);

        int indexQuery=url.indexOf("?");
        String queryString=url.substring(indexQuery+1);
        System.out.println(queryString);
        //根据&对字符串进行切分
        String[] kvs=queryString.split("&");
        System.out.println(Arrays.toString(kvs));
    }

    @Test
    public void testEncoder() throws Exception {
        String s1="a科计";
        //[97, -24, -82, -95, -25, -89, -111]
        byte[] utf8Bytes=  s1.getBytes("utf-8");
        System.out.println(Arrays.toString(utf8Bytes));
        //[97, -68, -58, -65, -58]
        byte[] gbkBytes=  s1.getBytes("GBK");
        System.out.println(Arrays.toString(gbkBytes));
         //采用当前源文件中默认的编码进行处理
        byte[] defaultBytes =s1.getBytes();
        System.out.println(Arrays.toString(defaultBytes));
    }

    @Test
    public void testDecoder() throws Exception {
        byte[] utf8Bytes= {97, -24, -82, -95, -25, -89, -111};
        //指定utf-8进行解码
        String s1 = new String(utf8Bytes, "utf-8");
        System.out.println(s1);
        //如果编码和解码的字符集不一致,则会出现乱码
        String s2 = new String(utf8Bytes, "GBK");
        System.out.println(s2);
    }

    @Test
    public void testEx()  throws Exception{
        try{
            String s1="抗日胜利80周年";
            byte[] bytes=  s1.getBytes("hehe");
            System.out.println("测试语句...");
        } catch (UnsupportedEncodingException e) {
            System.out.println("[额外功能]:记录日志 UnsupportedEncodingException");
            //throw e;
        } catch(Exception e){
            System.out.println("[额外功能]:记录日志 Exception");
            //throw e;
        }
    }
}
